package com.mxchip.livestar.ui.call


import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import com.mxchip.livestar.R
import com.mxchip.livestar.base.SystemData
import com.mxchip.livestar.base.hideSystemUI
import com.mxchip.livestar.base.toast
import com.mxchip.livestar.utils.MyLog
import com.mxchip.livestar.utils.Utils
import kotlinx.android.synthetic.main.dialog_hdmi_screen.*


class HdmiScreenDialogFragment : DialogFragment() {

    private val dbyViewModel by activityViewModels<DbyViewModel>()

    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.dialog_hdmi_screen, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        dbyViewModel.hdmi.observe(viewLifecycleOwner, Observer {
//            if (it){
//                button_video_only.visibility = View.GONE
//                button_video_and_audio.visibility = View.GONE
//                button_exit.visibility = View.VISIBLE
//
//            }else{
//                button_video_only.visibility = View.VISIBLE
//                button_video_and_audio.visibility = View.VISIBLE
//                button_exit.visibility = View.GONE
//            }
//        })
        dbyViewModel.openExtMic.observe(viewLifecycleOwner, Observer { enable ->
            if (enable) {
                button_ext_audio.text = getString(R.string.close_external_audio)
            } else {
                button_ext_audio.text = getString(R.string.open_external_audio)
            }
        })
        if (Utils.isApEnabled(requireContext())) {
            button_open_wifi_ap.visibility = View.GONE
        } else {
            button_open_wifi_ap.visibility = View.VISIBLE
        }
        dbyViewModel.shareScreen.observe(viewLifecycleOwner, Observer {
            when (it) {
                AVDevice.HDMI_RES -> {
                    button_video_only.visibility = View.GONE
                    button_video_and_audio.visibility = View.GONE
                    button_exit.visibility = View.VISIBLE
                    button_ext_audio.visibility = View.VISIBLE
                }
                AVDevice.WIFI_RES -> {
                    button_video_only.visibility = View.GONE
                    button_video_and_audio.visibility = View.GONE
                    button_exit.visibility = View.GONE
                    button_ext_audio.visibility = View.VISIBLE
                }
                AVDevice.NONE_RES -> {
                    button_video_only.visibility = View.VISIBLE
                    button_video_and_audio.visibility = View.VISIBLE
                    button_exit.visibility = View.GONE
                    button_ext_audio.visibility = View.GONE
                }
            }
        })

        button_video_only.setOnClickListener {
            if (SystemData.getInstance().wifiResPub) {
                toast(getString(R.string.can_not_use_wirelses))
                return@setOnClickListener
            }

            dbyViewModel.hdmi(true)
            dbyViewModel.extMic(false)

            findNavController().popBackStack()
        }

        button_video_and_audio.setOnClickListener {
            if (SystemData.getInstance().wifiResPub) {
                toast(getString(R.string.can_not_use_wirelses))
                return@setOnClickListener
            }

            dbyViewModel.hdmi(true)
            dbyViewModel.extMic(true)

            findNavController().popBackStack()
        }

        button_exit.setOnClickListener {
            if (SystemData.getInstance().wifiResPub) {
                toast(getString(R.string.can_not_use_wirelses))
                return@setOnClickListener
            }
            MyLog.logD(MyLog.HDMI_CAMERA_SEND_MODEL,"HdmiScreenDialogFragment::button_exit.setOnClickListener  ")
            dbyViewModel.hdmi(false)
            findNavController().popBackStack()
        }

        button_ext_audio.setOnClickListener {
            dbyViewModel.extMic(!dbyViewModel.openExtMic.value!!)
            findNavController().popBackStack()
        }

        button_open_wifi_ap.setOnClickListener {
            if (Utils.isWifiConnected(view.context)) {
                toast("请插入网线后,开启无线投屏功能")
                return@setOnClickListener
            }
            val wifiApEnabled = Utils.setWifiApEnabled(true, view.context)
            if (wifiApEnabled) toast("无线投屏功能已开启")
            findNavController().popBackStack()
        }

    }

    override fun onStart() {
        context?.apply {
            val width = resources.displayMetrics.widthPixels * 0.4
            val height = ViewGroup.LayoutParams.WRAP_CONTENT //resources.displayMetrics.heightPixels * 0.8
            dialog?.window?.setLayout(width.toInt(), height)
        }

        dialog?.window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)

        super.onStart()

        dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    }

    override fun onResume() {
        super.onResume()
        dialog?.window?.hideSystemUI()
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)

        dialog.window?.decorView?.setOnSystemUiVisibilityChangeListener { visibility ->
            if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
                // adjustments to your UI, such as showing the action bar or
                // other navigational controls.
                dialog.window?.hideSystemUI()
            } else {
                // adjustments to your UI, such as hiding the action bar or
                // other navigational controls.
            }
        }
        return dialog
    }


    companion object {
        private const val TAG = "HdmiScreenDialogFragment"
    }

}
